#!/bin/bash
# 
# Description: Downgrade Abiword to version 2.8.6
# Author: Wilson Santos

sudo apt-get purge abiword libabiword-2.9 -y --force-yes

# Find machine architecture
if [ `uname -m` == "x86_64" ]; then
  arch="amd64"
else
  arch="i386"
fi

abiword_common_deb="abiword-common_2.8.6-0.3ubuntu2_all.deb"
abiword_plugin_grammar_deb="abiword-plugin-grammar_2.8.6-0.3ubuntu2_$arch.deb"
abiword_plugin_mathview_deb="abiword-plugin-mathview_2.8.6-0.3ubuntu2_$arch.deb"
abiword_deb="abiword_2.8.6-0.3ubuntu2_$arch.deb"
libabiword_deb="libabiword-2.8_2.8.6-0.3ubuntu2_$arch.deb"
libwv_deb="libwv-1.2-3_1.2.4-2ubuntu3_$arch.deb"

prefix_url="https://launchpad.net/ubuntu/+archive/primary/+files/"
abiword_common_url="$prefix_url$abiword_common_deb"
abiword_plugin_grammar_url="$prefix_url$abiword_plugin_grammar_deb"
abiword_plugin_mathview_url="$prefix_url$abiword_plugin_mathview_deb"
abiword_url="$prefix_url$abiword_deb"
libabiword_url="$prefix_url$libabiword_deb"
libwv_url="$prefix_url$libwv_deb"

# Download the deb files
cd /tmp
wget "$abiword_common_url" "$abiword_plugin_grammar_url" "$abiword_plugin_mathview_url" "$abiword_url" "$libabiword_url" "$libwv_url"

# Install dependencies
sudo apt-get install ttf-lyx libaiksaurusgtk-1.2-0c2a libgoffice-0.8-8 libjpeg62 libgdome2-0 libgdome2-cpp-smart0c2a libgtkmathview0c2a liblink-grammar4 libots0 libt1-5 libwmf0.2-7 link-grammar-dictionaries-en -y
# Install the deb files
sudo dpkg -i "$abiword_common_deb" "$abiword_plugin_grammar_deb" "$abiword_plugin_mathview_deb" "$abiword_deb" "$libabiword_deb" "$libwv_deb"

# Lock Abiword version so it will not be upgraded
echo "abiword hold" | sudo dpkg --set-selections
echo "abiword-plugin-grammar hold" | sudo dpkg --set-selections
echo "abiword-plugin-mathview hold" | sudo dpkg --set-selections
echo "abiword-common hold" | sudo dpkg --set-selections
echo "libabiword-2.8 hold" | sudo dpkg --set-selections

# Remove the temporary deb files
rm -f "$abiword_common_deb" "$abiword_plugin_grammar_deb" "$abiword_plugin_mathview_deb" "$abiword_deb" "$libabiword_deb" "$libwv_deb"
